import React, { useState } from 'react'
import Navbar from '../../compoents/Navbar'
import style from './index.module.css'
import { useNavigate } from 'react-router-dom'
import { Card, Tabs, Popup } from 'antd-mobile'
import NameCard from '../../compoents/NameCard'
function Index() {
    const navigate = useNavigate()
    const [visible1, setVisible1] = useState(false)
    const [able,setAble] = useState(false)
    // 模拟档案数据
    const [currentPatient, setCurrentPatient] = useState({
        name: "王小柯",
        namecode: "20151234567890",
        reports: [
            {
                title: "双肾及肾血管彩色多普勒",
                time: "2020-01-01 10:20:36",
                status: "报告已出",
                statusColor: "#4A7CE8"
            },
            {
                title: "上腹部CT平扫",
                time: "2020-01-01 10:20:36", 
                status: "报告已出",
                statusColor: "#4A7CE8"
            },
            {
                title: "核磁共振",
                time: "--",
                status: "报告未出",
                statusColor: "#FF4D4F"
            }
        ],
        labReports: [
            {
                title: "血常规",
                time: "2020-01-01 10:20:36",
                status: "报告已出",
                statusColor: "#4A7CE8"
            },
            {
                title: "肝功三项",
                time: "--",
                status: "报告未出",
                statusColor: "#FF4D4F"
            }
        ]
    })

    // 患者列表数据
    const patientList = [
        {
            id: 1,
            name: "王小柯",
            namecode: "20151234567890",
            reports: [
                {
                    title: "双肾及肾血管彩色多普勒",
                    time: "2020-01-01 10:20:36",
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "上腹部CT平扫",
                    time: "2020-01-01 10:20:36", 
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "核磁共振",
                    time: "--",
                    status: "报告未出",
                    statusColor: "#FF4D4F"
                }
            ],
            labReports: [
                {
                    title: "血常规",
                    time: "2020-01-01 10:20:36",
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "肝功三项",
                    time: "--",
                    status: "报告未出",
                    statusColor: "#FF4D4F"
                }
            ]
        },
        {
            id: 2,
            name: "李玲玲", 
            namecode: "13124346575686789",
            reports: [
                {
                    title: "心电图检查",
                    time: "2020-02-15 14:30:20",
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "胸部X光片",
                    time: "2020-02-15 14:30:20",
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "血常规检查",
                    time: "2020-02-16 09:15:45",
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "肝功能检查",
                    time: "--",
                    status: "报告未出",
                    statusColor: "#FF4D4F"
                }
            ],
            labReports: [
                {
                    title: "尿常规",
                    time: "2020-02-15 14:30:20",
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "血糖检测",
                    time: "2020-02-16 09:15:45",
                    status: "报告已出",
                    statusColor: "#4A7CE8"
                },
                {
                    title: "血脂四项",
                    time: "--",
                    status: "报告未出",
                    statusColor: "#FF4D4F"
                }
            ]
        }
    ]

    // 格式化namecode：只取十位数字，中间四位变成*号
    const formatNamecode = (namecode: string) => {
        if (!namecode) return "";
        const numbers = namecode.replace(/\D/g, "");
        const tenDigits = numbers.substring(0, 10);
        if (tenDigits.length < 10) return tenDigits;
        return tenDigits.substring(0, 4) + "****" + tenDigits.substring(8);
    }

    // 选择患者
    const selectPatient = (patient: typeof currentPatient) => {
        setCurrentPatient(patient);
         if(currentPatient.name==='王小柯'){
            console.log(11);
            
         }
        setVisible1(false);
        if(currentPatient.name === '李玲玲'){
            setAble(true)
        }
    }

    const goDoorDetail = (id: number) => {
        if(currentPatient.reports[0].status === "报告已出"){
            navigate(`/DoorDetail/${id}`);
        }
    }

    const goDoorDetails = (id: number) => {
        if(currentPatient.reports[0].status === "报告未出"){
            navigate(`/DoorDetails/${id}`);
        }
    }

    return (
        <div className={style.container}>
            <Navbar title="门诊报告" showBack={true} onBack={() => navigate(-1)} />
            
            <div className={style.content}>
                {/* 患者信息卡片 */}


              <NameCard />
            

                {/* 标签页 */}
                <Card className={style.tabCard}>
                    <Tabs>
                        <Tabs.Tab title="检查" key="检查">
                            <div className={style.reportList}>
                                {currentPatient.reports.map((report, index) => (
                                    <div key={index} className={style.reportItem} onClick={()=>goDoorDetail(index)}>
                                        <div className={style.reportLeft}>
                                            <div className={style.reportTitle}>{report.title}</div>
                                            <div className={style.reportTime}>报告时间: {report.time}</div>
                                        </div>
                                        <div className={style.reportStatus} style={{color: report.statusColor}}>
                                            {report.status}
                                        </div>
                                    </div>
                                ))}
                            </div>
                        </Tabs.Tab>
                        <Tabs.Tab title="检验" key="检验">
                            <div className={style.reportList}>
                                {currentPatient.labReports.map((report, index) => (
                                    <div key={index} className={style.reportItem} onClick={()=>goDoorDetails(index)}>
                                        <div className={style.reportLeft}>
                                            <div className={style.reportTitle}>{report.title}</div>
                                            <div className={style.reportTime}>报告时间: {report.time}</div>
                                        </div>
                                        <div className={style.reportStatus} style={{color: report.statusColor}}>
                                            {report.status}
                                        </div>
                                    </div>
                                ))}
                            </div>
                        </Tabs.Tab>
                    </Tabs>
                </Card>
            </div>
        </div>
    )
}

export default Index
